//
//  DabiaoViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/7/6.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "DabiaoViewController.h"
#import "DabiaoTableViewCell.h"

#define kCellName @"DabiaoTableViewCell"

@interface DabiaoViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL _isYidaibao;
    NSString *_type;
}


@property (nonatomic,strong) NSMutableArray *yidabiaoArray;

@property (nonatomic,strong) NSMutableArray *weidabiaoArray;
@end

@implementation DabiaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self UISetting];
    [self loadData];
}

- (void)UISetting{
    _yidabiaoArray = [NSMutableArray array];
    _weidabiaoArray = [NSMutableArray array];
    _isYidaibao = YES;
    
//    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        [self loadData];
//        [_tableView.mj_header endRefreshing];
//    }];
}

#pragma mark - 网络请求

- (void)loadData{
    [self.hub hideAnimated:YES];
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    // Set the label text.
    self.hub.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
    [[AFClient shareInstance] dabiaoByEid:kUserId page_size:@"1000" page_index:@"0" progressBlock:nil success:^(id responseBody) {
        [self.hub hideAnimated:YES];
        if ([responseBody[@"code"] integerValue] == 200) {
            
            [_yidabiaoArray removeAllObjects];
            [_weidabiaoArray removeAllObjects];
            
            for (NSDictionary *dic in responseBody[@"data"]) {
                DabiaoModel *model = [[DabiaoModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                
                if (model.is_ok.integerValue >0) {
                    [_yidabiaoArray addObject:model];
                }else{
                    [_weidabiaoArray addObject:model];
                }
            }
            if (_isYidaibao) {
                [self dabiaoButtonClick:_buttonYiDaBiao];
            }else{
                [self dabiaoButtonClick:_buttonWeiDaBiao];
            }
        }else{
            kAlert(responseBody[@"message"]);
        }
    } failure:^(NSError *error) {
        [self.hub hideAnimated:YES];
    }];
}

#pragma mark - 点击事件
- (IBAction)backButtonClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


//101已达标，102未达标
- (IBAction)dabiaoButtonClick:(UIButton *)sender {
    if (sender.tag == 101) {
        _isYidaibao = YES;
    }else{
        _isYidaibao = NO;
    }
    [self dabiaoStyle:sender.tag];
    [_tableView reloadData];
}

- (void)dabiaoStyle:(NSInteger)buttonTag{
    if (buttonTag == 101) {
        [_buttonYiDaBiao setBackgroundColor:kWhiteColor];
        [_buttonYiDaBiao setTitleColor:kGreenColor forState:UIControlStateNormal];
        
        [_buttonWeiDaBiao setBackgroundColor:kColorFrom0x(0xe8e8e8)];
        [_buttonWeiDaBiao setTitleColor:kColorFrom0x(0xb5b5b5) forState:UIControlStateNormal];
    }else{
        [_buttonWeiDaBiao setBackgroundColor:kWhiteColor];
        [_buttonWeiDaBiao setTitleColor:kGreenColor forState:UIControlStateNormal];
        
        [_buttonYiDaBiao setBackgroundColor:kColorFrom0x(0xe8e8e8)];
        [_buttonYiDaBiao setTitleColor:kColorFrom0x(0xb5b5b5) forState:UIControlStateNormal];
    }
}

#pragma mark - tableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_isYidaibao) {
        return _yidabiaoArray.count;
    }else{
        return _weidabiaoArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DabiaoTableViewCell *cell = (DabiaoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kCellName];
    DabiaoModel *model ;
    if (_isYidaibao) {
        model = _yidabiaoArray[indexPath.row];
        [cell showDataWithModel:model];
    }else{
        model = _weidabiaoArray[indexPath.row];
        [cell showDataWithModel:model];
    }
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
